package com.zxmc.server.controller.user;

import com.aliyun.oss.AliOSSUtils;
import com.zxmc.common.result.Result;
import com.zxmc.pojo.entity.Admin;
import com.zxmc.pojo.entity.User;
import com.zxmc.server.service.AdminService;
import com.zxmc.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private AliOSSUtils ossUtils;
    @PostMapping("/user/upload")
    public Result uploadUser(MultipartFile image) throws IOException {
        User user = userService.list();
        String url = user.getAvatar();
        if(image!=null){
            url = ossUtils.upload(image);
        }
        return Result.success(url);

    }
    @PostMapping("/admin/upload")
    public Result uploadAdmin(MultipartFile image) throws IOException {
        Admin admin = adminService.list();
        String url = admin.getAvatar();
        if(image!=null){
            url = ossUtils.upload(image);
        }
        return Result.success(url);

    }
}
